package vm;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Paulius Dambrauskas
 */
public class ProgramFile {
    public String content = "NULL";
    public ProgramFile(File file){
       if(file.isFile()){
            if(getExtension(file).equals("vmc")){
              try{
                  content = readFile(file);
             } catch (IOException e){
                 content = "NULL";
                }
            }
        }
    }
    private String readFile(File file) throws IOException {
        StringBuilder fileContents = new StringBuilder((int)file.length());
        Scanner scanner = new Scanner(file);
        String lineSeparator = System.getProperty("line.separator");
        try {
            while(scanner.hasNextLine()) {        
                fileContents.append(scanner.nextLine() + lineSeparator);
            }
            return fileContents.toString();
        } finally {
            scanner.close();
        }
    }
    public String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}
